﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Linq;
using Microsoft.Win32;
using System.Windows.Forms;

namespace EvoGame.Ceeforee.Helpers
{
	public static partial class Helper
	{
		private static MainWin mWin;

		public static void SetMWin(MainWin mw)
		{
			mWin = mw;
		}

		public static DirectoryInfo[] GetMods(CivX mode)
		{
			DirectoryInfo modDir = new DirectoryInfo(CivDir(mode)+"\\Mods");
			DirectoryInfo[] mods;
			try
			{
				 mods = modDir.GetDirectories();
			}
			catch (DirectoryNotFoundException dnf)
			{
				mWin.Debug = dnf.Message;
				mWin.Debug = "Civ Directory not Found. Aborting...";
				Application.Exit();
				mods = null;
			}
			return mods;
		}

		public static string CivDir(CivX mode)
		{
			StringBuilder civDir = new StringBuilder();
			civDir.Append(Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Firaxis Games").OpenSubKey("Sid Meier\'s Civilization 4").GetValue("InstallDir", "C:\\Program Files\\Firaxis Games\\Sid Meier\'s Civilization 4").ToString());
			switch (mode)
			{
				default:
				case CivX.Vanilla:
					break;
				case CivX.Warlords:
					civDir.Append("\\Warlords");
					break;
				case CivX.BeyondTheSword:
					civDir.Append("\\Beyond the Sword");
					break;
			}
			return civDir.ToString();
		}

		public static string HashString(string Value)
		{
			System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
			byte[] data = System.Text.Encoding.ASCII.GetBytes(Value);
			data = x.ComputeHash(data);
			string ret = "";
			for (int i = 0; i < data.Length; i++)
				ret += data[i].ToString("x2").ToLower();
			return ret;
		}

		[Serializable]
		public class Vector<T>
		{
			public T X { get { return _x; } }
			public T Y { get { return _y; } }
			public T Z { get { return _z; } }
			public T W { get { return _w; } }

			private T _x, _y, _z, _w;

			public Vector(T x, T y)
			{
				_x = x;
				_y = y;
				_z = default(T);
				_w = default(T);
			}
			public Vector(T x, T y, T z)
			{
				_x = x;
				_y = y;
				_z = z;
				_w = default(T);
			}
			public Vector(T x, T y, T z, T w)
			{
				_x = x;
				_y = y;
				_z = z;
				_w = w;
			}

		}
	}
}
